home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.3 Development Libraries / SGI IRIX 6.3 Development Libraries.iso / dist6.3 / gl_dev.idb / usr / share / src / OpenGL / teach / texture / sharpen.c.z / sharpen.c
Encoding:
C/C++ Source or Header  |  1996-12-06  |  4.3 KB  |  158 lines

  1. /*
  2.  * sharpen - simple program for texture sharpening
  3.  *
  4.  * Press l key for bilinear magnification
  5.  *       a key to sharpen alpha
  6.  *       c key to sharpen color
  7.  *       s key to sharpen alpha and color
  8.  */
  9. /* compile: cc -o sharpen sharpen.c -lGLU -lGL -lX11 */
  10.  
  11. #include <GL/glx.h>
  12. #include <GL/glu.h>
  13. #include <X11/keysym.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16.  
  17. /* tree texture: high alpha in foreground, zero alpha in background */
  18. #define B 0x00000000
  19. #define F 0xA0A0A0ff
  20. unsigned int tex[] = {
  21.     B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,
  22.     B,B,B,B,B,B,B,F,F,B,B,B,B,B,B,B,
  23.     B,B,B,B,B,B,B,F,F,B,B,B,B,B,B,B,
  24.     B,B,B,B,B,B,F,F,F,F,B,B,B,B,B,B,
  25.     B,B,B,B,B,B,F,F,F,F,B,B,B,B,B,B,
  26.     B,B,B,B,B,F,F,F,F,F,F,B,B,B,B,B,
  27.     B,B,B,B,B,F,F,F,F,F,F,B,B,B,B,B,
  28.     B,B,B,B,F,F,F,F,F,F,F,F,B,B,B,B,
  29.     B,B,B,B,F,F,F,F,F,F,F,F,B,B,B,B,
  30.     B,B,B,F,F,F,F,F,F,F,F,F,F,B,B,B,
  31.     B,B,B,F,F,F,F,F,F,F,F,F,F,B,B,B,
  32.     B,B,F,F,F,F,F,F,F,F,F,F,F,F,B,B,
  33.     B,B,F,F,F,F,F,F,F,F,F,F,F,F,B,B,
  34.     B,B,B,B,B,B,F,F,F,F,B,B,B,B,B,B,
  35.     B,B,B,B,B,B,F,F,F,F,B,B,B,B,B,B,
  36.     B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,
  37. };
  38.  
  39. static void
  40. init(void) {
  41.     glEnable(GL_TEXTURE_2D);
  42.     glMatrixMode(GL_PROJECTION);
  43.     gluPerspective(60.0, 1.0, 1.0, 10.0 );
  44.     glMatrixMode(GL_MODELVIEW);
  45.     glTranslatef(0.,0.,-2.5);
  46.  
  47.     glColor4f(0,0,0,1);
  48.     glClearColor(0.0, 0.0, 0.0, 1.0);
  49.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  50.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  51.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  52.     /* sharpening just alpha is useful for keeping the tree outline crisp */
  53.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
  54.             GL_LINEAR_SHARPEN_ALPHA_SGIS);
  55.     /* generate mipmaps; levels 0 and 1 are needed for sharpening */
  56.     gluBuild2DMipmaps(GL_TEXTURE_2D, 4, 16, 16, GL_RGBA, GL_UNSIGNED_BYTE,
  57.               tex);
  58. }
  59.  
  60. static void
  61. draw_scene(void) {
  62.     glClear(GL_COLOR_BUFFER_BIT);
  63.     glBegin(GL_TRIANGLE_STRIP);
  64.         glTexCoord2f( 0, 1); glVertex2f(-1,-1); 
  65.         glTexCoord2f( 0, 0); glVertex2f(-1, 1); 
  66.         glTexCoord2f( 1, 1); glVertex2f( 1,-1); 
  67.         glTexCoord2f( 1, 0); glVertex2f( 1, 1); 
  68.     glEnd();
  69.     glFlush();
  70. }
  71.  
  72. static void
  73. process_input(Display *dpy) {
  74.     XEvent event;
  75.     Bool redraw = 0;
  76.  
  77.     do {
  78.     char buf[31];
  79.     KeySym keysym;
  80.  
  81.     XNextEvent(dpy, &event);
  82.     switch(event.type) {
  83.       case Expose:
  84.         redraw = 1;
  85.         break;
  86.       case ConfigureNotify:
  87.         glViewport(0, 0, event.xconfigure.width, event.xconfigure.height);
  88.         redraw = 1;
  89.         break;
  90.       case KeyPress:
  91.         (void) XLookupString(&event.xkey, buf, sizeof(buf), &keysym, NULL);
  92.         redraw = 1;
  93.         switch (keysym) {
  94.           case XK_s:
  95.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
  96.                 GL_LINEAR_SHARPEN_SGIS);
  97.         break;
  98.           case XK_a:
  99.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
  100.                 GL_LINEAR_SHARPEN_ALPHA_SGIS);
  101.         break;
  102.           case XK_c:
  103.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
  104.                 GL_LINEAR_SHARPEN_COLOR_SGIS);
  105.         break;
  106.           case XK_l:
  107.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
  108.                 GL_LINEAR);
  109.         break;
  110.           case XK_Escape:
  111.         exit(EXIT_SUCCESS);
  112.           default:
  113.         break;
  114.         }
  115.       default:
  116.         break;
  117.     }
  118.     } while (XPending(dpy));
  119.     if (redraw) draw_scene();
  120. }
  121.  
  122. static void
  123. error(const char *prog, const char *msg) {
  124.     fprintf(stderr, "%s: %s\n", prog, msg);
  125.     exit(EXIT_FAILURE);
  126. }
  127.  
  128. static int attributeList[] = { GLX_RGBA, None };
  129.  
  130. int
  131. main(int argc, char **argv) {
  132.     Display *dpy;
  133.     XVisualInfo *vi;
  134.     XSetWindowAttributes swa;
  135.     Window win;
  136.     GLXContext cx;
  137.  
  138.     dpy = XOpenDisplay(0);
  139.     if (!dpy) error(argv[0], "can't open display");
  140.     vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList);
  141.     if (!vi) error(argv[0], "no suitable visual");
  142.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  143.  
  144.     swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
  145.                                    vi->visual, AllocNone);
  146.     swa.border_pixel = 0;
  147.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask;
  148.     win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 800, 800,
  149.             0, vi->depth, InputOutput, vi->visual,
  150.             CWBorderPixel|CWColormap|CWEventMask, &swa);
  151.     XStoreName(dpy, win, "sharpen");
  152.     XMapWindow(dpy, win);
  153.     glXMakeCurrent(dpy, win, cx);
  154.  
  155.     init();
  156.     while (1) process_input(dpy);
  157. }
  158.